# field_management/forms.py
from django import forms
from .models import Field
from django.utils.translation import gettext_lazy as _

class FieldForm(forms.ModelForm):
    class Meta:
        model = Field
        # Exclude owner, created_at, updated_at as they are handled automatically/by view
        fields = ['name', 'location', 'latitude', 'longitude', 'area', 'soil_type', 'irrigation_source', 'notes']
        widgets = {
            'notes': forms.Textarea(attrs={'rows': 3}),
            'area': forms.NumberInput(attrs={'step': '0.01', 'min': '0.01'}),
            'latitude': forms.NumberInput(attrs={'step': '0.000001'}),
            'longitude': forms.NumberInput(attrs={'step': '0.000001'}),
        }
        labels = { # Labels can also be defined in the model verbose_name
            'name': _('田块名称/编号'),
            'location': _('位置描述'),
            'latitude': _('纬度'),
            'longitude': _('经度'),
            'area': _('面积 (亩)'),
            'soil_type': _('土壤类型'),
            'irrigation_source': _('灌溉来源'),
            'notes': _('备注'),
        }
        help_texts = {
            'area': _('请输入大于0的数字，单位为亩。'),
            'latitude': _('可选，例如 31.2304'),
            'longitude': _('可选，例如 121.4737'),
        }

    def clean_area(self):
        area = self.cleaned_data.get('area')
        if area is not None and area <= 0:
            raise forms.ValidationError(_("面积必须大于 0。"))
        return area